package com.mcc.app.group.controller;

import cn.hutool.core.date.DateUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mcc.app.group.domain.GroupSche;
import com.mcc.app.group.dto.GroupScheAutoDTO;
import com.mcc.app.group.dto.GroupScheBatchDTO;
import com.mcc.app.group.service.IGroupScheService;
import com.mcc.common.annotation.Log;
import com.mcc.common.core.controller.BaseController;
import com.mcc.common.core.domain.AjaxResult;
import com.mcc.common.core.page.TableDataInfo;
import com.mcc.common.enums.BusinessType;
import com.mcc.common.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 排班计划
 *
 * @author gxy
 * @date 2021-07-08
 */
@Api(tags = "排班计划")
@RestController
@RequestMapping("/group/sche")
public class GroupScheController extends BaseController {

    @Autowired
    private IGroupScheService groupScheService;

    @ApiOperation("查询排班计划列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/list")
    public AjaxResult<TableDataInfo<GroupSche>> list(GroupSche groupSche) {
        List<GroupSche> list = groupScheService.selectGroupScheList(groupSche);
        return getDataTable(list);
    }

    @ApiOperation("查询排班计划列表(分页)")
    @ApiOperationSupport(order = 2)
    @GetMapping("/page")
    public AjaxResult<TableDataInfo<GroupSche>> page(GroupSche groupSche) {
        startPage();
        List<GroupSche> list = groupScheService.selectGroupScheList(groupSche);
        return getDataTable(list);
    }

    @ApiOperation("获取排班计划详细信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/get/{scheId}")
    public AjaxResult<GroupSche> getInfo(@PathVariable("scheId") Long scheId) {
        return AjaxResult.success(groupScheService.selectGroupScheById(scheId));
    }

    @ApiOperation("新增排班计划")
    @ApiOperationSupport(order = 4)
    @Log(title = "排班计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GroupSche groupSche) {
        if (!groupScheService.checkGroupSche(groupSche)) {
            throw new CustomException("该时间段已有排班");
        }
        groupScheService.calTimeGroupSche(groupSche);
        return toAjax(groupScheService.insertGroupSche(groupSche));
    }

    @ApiOperation("修改排班计划")
    @ApiOperationSupport(order = 5)
    @Log(title = "排班计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody GroupSche groupSche) {
        if (!groupScheService.checkGroupSche(groupSche)) {
            throw new CustomException("该时间段已有排班");
        }
        groupScheService.calTimeGroupSche(groupSche);
        return toAjax(groupScheService.updateGroupSche(groupSche));
    }

    @ApiOperation("删除排班计划")
    @ApiOperationSupport(order = 6)
    @Log(title = "排班计划", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{scheIds}")
    public AjaxResult remove(@PathVariable Long[] scheIds) {
        return toAjax(groupScheService.deleteGroupScheByIds(scheIds));
    }

    @ApiOperation("批量排班计划")
    @ApiOperationSupport(order = 7)
    @PostMapping("/batchDaySche")
    public AjaxResult batchDaySche(@RequestBody GroupScheBatchDTO groupScheBatch) {
        if (!groupScheService.checkGroupScheBatch(groupScheBatch)) {
            return AjaxResult.error("该时间段已有排班");
        }
        return toAjax(groupScheService.batchDaySche(groupScheBatch));
    }

    @ApiOperation("自动排班设置")
    @ApiOperationSupport(order = 8)
    @PostMapping("/setAutoSche")
    public AjaxResult setAutoSche(@RequestBody GroupScheAutoDTO groupScheAuto) {
        return toAjax(groupScheService.setAutoSche(groupScheAuto));
    }

    @ApiOperation("获取自动排班设置")
    @ApiOperationSupport(order = 9)
    @GetMapping("/getAutoSche")
    public AjaxResult<GroupScheAutoDTO> getAutoSche() {
        return AjaxResult.success(groupScheService.getAutoSche());
    }

    @ApiOperation("获取当前排班")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getCurrentSche")
    public AjaxResult<GroupSche> getCurrentSche() {
        return AjaxResult.success(groupScheService.getGroupScheByDt(new Date()));
    }

    @ApiOperation("获取上一个排班")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getLastSche")
    public AjaxResult<GroupSche> getLastSche(){
        Date date = new Date();
        Date groupDate = DateUtil.offsetHour(date, -8);//为准确获取当前结束得是哪个班次
        GroupSche groupSche = groupScheService.getGroupScheByDt(groupDate);
        return AjaxResult.success(groupSche);
    }
}
